package com.booktrading.service.impl;

import com.booktrading.mapper.BookMapper;
import com.booktrading.model.Book;
import com.booktrading.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;
    
    @Override
    public Book publishBook(Book book) {
        // 设置初始值
        book.setStatus(1); // 1-在售
        book.setViews(0);
        book.setCreateTime(new Date());
        book.setUpdateTime(new Date());
        
        // 插入数据
        bookMapper.insert(book);
        return book;
    }

    @Override
    public Book getBookById(Long id) {
        return bookMapper.selectById(id);
    }

    @Override
    public List<Book> getBooksBySeller(Long sellerId) {
        return bookMapper.selectBySellerId(sellerId);
    }

    @Override
    public boolean updateBook(Book book) {
        book.setUpdateTime(new Date());
        return bookMapper.updateById(book) > 0;
    }

    @Override
    public boolean updateBookStatus(Long id, Integer status) {
        return bookMapper.updateStatus(id, status) > 0;
    }

    @Override
    public List<Book> getOnSaleBooks() {
        return bookMapper.selectOnSale();
    }

    @Override
    public List<Book> searchBooks(String keyword) {
        return bookMapper.search(keyword);
    }

    @Override
    public List<Book> getAllOnSaleBooks() {
        return bookMapper.selectOnSale();
    }

    @Override
    public List<Book> getBooksByCondition(Book condition) {
        return bookMapper.selectByCondition(condition);
    }

    @Override
    public List<Book> getBooksByCategory(String category) {
        return bookMapper.selectByCategory(category);
    }

    @Override
    public List<Book> getBooksByPriceRange(Double minPrice, Double maxPrice) {
        return bookMapper.selectByPriceRange(minPrice, maxPrice);
    }

    @Override
    public List<Book> getBooksByStatus(Integer status) {
        return bookMapper.selectByStatus(status);
    }

    @Override
    public boolean deleteBook(Long id) {
        return bookMapper.deleteById(id) > 0;
    }

    @Override
    public boolean incrementViews(Long id) {
        return bookMapper.incrementViews(id) > 0;
    }
} 